#include "Observer.h"

void Subject::SetAndNotifyState(STATE aState)
{
	//TemplateMethodģʽ
	SetState(aState);	
	Notify();
	return;
}

void Subject::Register(Observer* pObserver)
{
	a_observerList.push_back(pObserver);
	return;
}

void Subject::Notify()
{
	vector<Observer*>::iterator it = a_observerList.begin();
	vector<Observer*>::iterator it_end = a_observerList.end();

	for (; it != it_end; ++it)
	{
		(*it)->Update(this);
	}
	return;
}


STATE Subject::GetState()
{
	return a_state;
}

void Subject::SetState(STATE aState)
{
	cout << "Subject::SetState" << endl;
	a_state = aState;
	return;
}


void ConcreteSubject::SetState(STATE aState)
{
	cout << "ConcreteSubject::SetState" << endl;
	a_state = aState;
	return;
}

void ConcreteObserver1::Update(Subject* pSubject)
{
	a_state = pSubject->GetState();
	cout << "ConcreteObserver1::update, state= " << a_state << endl;
	return;
}

void ConcreteObserver2::Update(Subject* pSubject)
{
	a_state = pSubject->GetState();
	cout << "ConcreteObserver2::update, state= " << a_state << endl;
	return;
}
